// 获取token值
var token = getCookie(tokenName);

layui.define(['jquery','toast'],function(exports){ //提示：模块也可以依赖其它模块，如：layui.define('layer', callback);

    var $ = layui.jquery;
    var toast = layui.toast;

    var obj = {
        sendPost: function(url,data,async,isToken){
            if (isToken) {
                return this.sendAndToken(baseUrl+url,data,"POST",async);
            }else {
                return this.sendNotToken(baseUrl+url,data,"POST",async);
            }
        },
        sendGet: function(url,data,async,isToken){
            if (isToken) {
                return this.sendAndToken(baseUrl+url,data,"GET",async);
            }else {
                return this.sendNotToken(baseUrl+url,data,"GET",async);
            }
        },
        /**
         *
         * @param url       地址
         * @param data      数据
         * @param type      类型：POST、GET
         * @param async     是否异步请求
         */
        sendNotToken: function (url,data,type,async){
            var result = null;
            $.ajax({
                url:url,
                data:data,
                type:type,
                async:async,
                success:function (res) {
                    // 600 表示没有登录，直接跳转登录页
                    if (res.status == 500) {
                        toast.error({title: 'Error',message: res.msg,position: 'topRight'});
                        return  false;
                    }else {
                        result =  res;
                    }
                },
                error:function (res) {
                    if (res.status == 500) {
                        toast.error({title: 'Error',message:  res.responseJSON.message,position: 'topRight'});
                        return false;
                    }else {
                        toast.error({title: 'Error',message: '系统出现错误,请稍后重试！',position: 'topRight'});
                        return false;
                    }

                }
            });
            // console.log(result);
            return result;
        },
        /**
         *
         * @param url       地址
         * @param data      数据
         * @param type      类型：POST、GET
         * @param async     是否异步请求
         */
        sendAndToken: function (url,data,type,async){
            var result = null;
            $.ajax({
                url:url,
                data:data,
                type:type,
                async:async,
                beforeSend:function (request) {
                    request.setRequestHeader(tokenName,tokenPrefix + token);
                },
                success:function (res) {
                    // 600 表示没有登录，直接跳转登录页
                    if (res.status == 600) {
                        window.location.href = loginUrl;
                        return  false;
                    }else if (res.status == 500) {
                        toast.error({title: 'Error',message: res.msg,position: 'topRight'});
                        return  false;
                    }else {
                        result =  res;
                    }
                },
                error:function (res) {
                    var status =  res.status;
                    if (status == 406) {
                        toast.error({title: 'Error',message: '未登录',position: 'topRight'});
                    }else if (status == 500) {
                        toast.error({title: 'Error',message:  res.responseJSON.message,position: 'topRight'});
                    }else {
                        toast.error({title: 'Error',message: '系统出现错误,请稍后重试！',position: 'topRight'});
                    }
                    return false;
                }
            });
            return result;
        }
    };

    //输出接口
    exports('ajaxRequest', obj);
});



/**
 * 不携带token的方式,发送一个JSON对象，controller必须加@ResponsBody接收（不使用）
 */
function sendNotBean(url,data,async){
    var result = null;
    $.ajax({
        url: url,
        data:JSON.stringify(data),
        type:'POST',
        async:async,
        contentType:"application/json;charset=UTF-8",
        success:function (res) {
            if (res.status == 400) {
                $.each(res.data,function (index,item) {
                    layer.msg(item);
                    return false;
                })
            }else if (res.status != 200) {
                layer.msg(res.msg);
                return  false;
            } else{
                result = res;
            }
        },
        error:function (res) {
            layer.msg("系统出现错误");
            return false;
        }
    });
    return result;
}

